//获取应用的实例
var app = getApp();
Page({
  data: {
    userInfo: {},
    imgUrls: [
      'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
    ],
    indicatorDots: true,
    autoplay: true,
    interval: 2000,
    duration: 1000,
    circular: true,
    bookDatas:[],
    scrollHeight:'',
    scrollWidth:'',

    translateMaxX:'',
    // 侧边菜单移动的距离
    translateX: '',
    translateY: '',
    transition: '',
    // 刚开始让侧边菜单隐藏
    sidemenuShow: 'none',
    // 遮罩上触摸的点
    startPositionX: '',
    startPositionY: '',
    endPositionX: '',
    movingDistance: '',
    // 遮罩的显示与隐藏
    show: 'none',

    // 小球
    ballStartPositionX:'',
    ballStartPositionY:'',
    ballPositionX:'',
    ballPositionY:'',
    ballShow:'block'
  },
  onLoad: function() {
  	var _this = this;
  	console.log(this.data.bookDatas);
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function(userInfo){
      //更新数据
      _this.setData({
        userInfo:userInfo
      })
    });
    // 得到系统信息
    wx.getSystemInfo({
      success: function(res) {
        console.log(res.windowWidth);
        console.log(res.windowHeight); 
        _this.setData({
          scrollHeight: res.windowHeight,
          scrollWidth: res.windowWidth,
          translateMaxX: res.windowWidth*0.8,
          translateX: -res.windowWidth*0.8
        })
      }
    });
  	wx.request({
	  url: 'https://api.douban.com/v2/book/search?q=武侠&start=0&count=10',
	  success: function(res) {
	  	console.log(res);
	    _this.setData({
	    	bookDatas:res.data.books
	    })
	    console.log(_this.data.bookDatas);
	  }
	})
  },
  onShow: function() {
      this.setData({
        sidemenuShow: 'block'
      });
  },
  // 通过event向将要跳转过去的页面传递参数
  book_detail: function(event) {
  	var id = event.currentTarget.id;
  	console.log(id);
    wx.navigateTo({
        url: '../detail/detail?id='+id
    })
  },
  bindTouchStart: function(e) {
    var startPositionX = e.changedTouches[0].clientX;
    var startPositionY = e.changedTouches[0].clientY;
    var startTime =Date.now();
    this.setData({
      startPositionX:startPositionX,
      startPositionY:startPositionY
    });
  },
  bindTouchMoveHide: function(e) {
    var movePositionX = e.changedTouches[0].clientX;
    var movingDistance = this.data.startPositionX-movePositionX;
    if(movingDistance >= 0) {
      this.setData({
          transition: ''
        });
      if(movingDistance > this.data.translateMaxX/2) {
        movingDistance = this.data.translateMaxX;
        this.setData({
          transition: 'all 0.5s linear',
          show:'none',
          ballShow:'block'
        });
      };
      this.setData({
        translateX: -movingDistance,
        movingDistance: movingDistance
      });
    }
  },
  bindTouchEnd: function(e) {
    var endTime = Date.now();
    var endPositionX = e.changedTouches[0].clientX;
    var moveDistance = this.data.startPositionX-endPositionX;
    if(this.data.movingDistance >= 0) {
      if(moveDistance < this.data.translateMaxX/2) {
        this.setData({
          translateX:0,
          transition: 'all 0.5s linear',
          ballShow: 'none'
        });
      }
    }
  },
  scrollEvent: function() {
    
  },
  ballTouchStart: function(e) {
    var startPositionX = e.changedTouches[0].clientX;
    var startPositionY = e.changedTouches[0].clientY;
    this.setData({
       ballStartPositionX: startPositionX,
      ballStartPositionY: startPositionY,
    });
  },
  ballTouchMove: function(e) {
      var X = e.changedTouches[0].clientX;
      var Y = e.changedTouches[0].clientY;
      console.log(X);
      X = X <= 25? 25: (X > this.data.scrollWidth-25?this.data.scrollWidth-25:X);
      Y = Y <= 25? 25: (Y > this.data.scrollHeight-25?this.data.scrollHeight-25:Y);
      this.setData({
        ballPositionX: X-25,
        ballPositionY: Y-25 
      });
  },
  ballTap: function() {
     this.setData({
      translateX: 0,
      // 遮罩的显示与隐藏
      show: 'block',
      transition: 'all 0.5s linear',
      ballShow: 'none'
    });
  },
  searchTap: function() {
    wx.navigateTo({
        url: '../search/search'
    })
  },
  // 分享
  onShareAppMessage: function () {
    return {
      title: '分享',
      desc: '尝试一下分享',
      path: '/pages/detail/detail'
    }
  }
})